#pragma once
#include "qwindow.h"

using namespace micro::gui;
//#include "qdialog.h"

namespace micro
{



namespace dialog
{

class QTaskDialog //: public CObject
{

public:
    enum
    {
        PROGRESS_BAR_MIN = 0,
        PROGRESS_BAR_MAX = 100
    };

    QTaskDialog( const QString & strContent,  const QString & strMainInstruction,  const QString & strTitle, const QString & strFooter = L"",
                 int nCommonButtons = TDCBF_OK_BUTTON | TDCBF_CANCEL_BUTTON,  int nTaskDialogOptions = TDF_ENABLE_HYPERLINKS | TDF_USE_COMMAND_LINKS );

    QTaskDialog( const QString & strContent,  const QString & strMainInstruction,  const QString & strTitle, const QString & strFooter,
                 int nIDCommandControlsFirst ,  int nIDCommandControlsLast ,  int nCommonButtons ,
                 int nTaskDialogOptions = TDF_ENABLE_HYPERLINKS | TDF_USE_COMMAND_LINKS );

    virtual ~QTaskDialog();

    void LoadCommandControls( int nIDCommandControlsFirst,  int nIDCommandControlsLast );
    void LoadRadioButtons( int nIDRadioButtonsFirst,  int nIDRadioButtonsLast );

    void SetWindowTitle( const QString & strWindowTitle );
    void SetMainInstruction( const QString & strInstructions );
    void SetContent( const QString & strContent );
    void SetFooterText( const QString & strFooterText );
    void SetExpansionArea( const QString & strExpandedInformation,  const QString & strCollapsedLabel = L"",  const QString & strExpandedLabel = L"" );

    void SetMainIcon( HICON hMainIcon );
    void SetFooterIcon( HICON hFooterIcon );

    void SetMainIcon( LPCWSTR lpszMainIcon );
    void SetFooterIcon( LPCWSTR lpszFooterIcon );

    void SetDialogWidth( int nWidth = 0 );

    void SetOptions( int nOptionFlag );
    int GetOptions() const;

    void SetCommonButtons( int nButtonMask,  int nDisabledButtonMask = 0,  int nElevationButtonMask = 0 );
    void SetCommonButtonOptions( int nDisabledButtonMask,  int nElevationButtonMask = 0 );

    void AddCommandControl( int nCommandControlID,  const QString & strCaption,  BOOL bEnabled = TRUE,  BOOL bRequiresElevation = FALSE );
    void SetCommandControlOptions( int nCommandControlID,  BOOL bEnabled,  BOOL bRequiresElevation = FALSE );
    int GetSelectedCommandControlID() const;
    BOOL IsCommandControlEnabled( int nCommandControlID ) const;
    void SetDefaultCommandControl( int nCommandControlID );
    void RemoveAllCommandControls();

    void AddRadioButton( int nRadioButtonID,  const QString & strCaption,  BOOL bEnabled = TRUE );
    void SetRadioButtonOptions( int nRadioButtonID,  BOOL bEnabled );
    int GetSelectedRadioButtonID() const;
    BOOL IsRadioButtonEnabled( int nRadioButtonID ) const;
    void SetDefaultRadioButton( int nRadioButtonID );
    void RemoveAllRadioButtons();

    void SetVerificationCheckboxText( const QString & strVerificationText );
    void SetVerificationCheckbox( BOOL bChecked );
    BOOL GetVerificationCheckboxState() const;

    void SetProgressBarRange( int nRangeMin,  int nRangeMax );
    void SetProgressBarPosition( int nProgressPos );
    void SetProgressBarState( int nState = PBST_NORMAL );
    void SetProgressBarMarquee( BOOL bEnabled = TRUE,  int nMarqueeSpeed = 0 );
    void Notify( UINT uMsg,  WPARAM wParam,  LPARAM lParam ) const;

    INT_PTR DoModal( HWND hParent = ::GetActiveWindow() );

    //static BOOL IsSupported();
    static INT_PTR ShowDialog( const QString & strContent,  const QString & strMainInstruction,  const QString & strTitle,  int nIDCommandControlsFirst,  int nIDCommandControlsLast,
                               int nCommonButtons = TDCBF_YES_BUTTON | TDCBF_NO_BUTTON,  int nTaskDialogOptions = TDF_ENABLE_HYPERLINKS | TDF_USE_COMMAND_LINKS,  const QString & strFooter = QString() );

    friend HRESULT CALLBACK TaskDialogCallback( HWND hwnd,  UINT uNotification,  WPARAM wParam,  LPARAM lParam,  LONG_PTR dwRefData );

protected:
    void ClickRadioButton( int nRadioButtonID ) const;
    void ClickCommandControl( int nCommandControlID ) const;
    void NavigateTo( QTaskDialog & oTaskDialog ) const;

    virtual HRESULT OnCreate();
    virtual HRESULT OnInit();
    virtual HRESULT OnDestroy();
    virtual HRESULT OnCommandControlClick( int nCommandControlID );
    virtual HRESULT OnRadioButtonClick( int nRadioButtonID );
    virtual HRESULT OnVerificationCheckboxClick( BOOL bChecked );
    virtual HRESULT OnExpandButtonClick( BOOL bExpanded );
    virtual HRESULT OnHyperlinkClick( const QString & strHref );
    virtual HRESULT OnHelp();
    virtual HRESULT OnTimer( UINT_PTR id );
    virtual HRESULT OnNavigatePage();

    virtual int GetCommonButtonId( int nFlag ) const;
    virtual int GetCommonButtonFlag( int nButtonId ) const;
    virtual int GetCommonButtonCount() const;

private:
    QTaskDialog & operator=( const QTaskDialog & );

    enum
    {
        BUTTON_ENABLED = 0x01,
        BUTTON_ELEVATION = 0x02
    };

    struct QTaskDialogButton
    {
        int nId;
        QString strCaption;
        unsigned char cState;

        QTaskDialogButton( int nIdp = 0,  const QString & strCaptionParam = QString(),  unsigned char cStateParam = 0 )
            : nId ( nIdp ), strCaption ( strCaptionParam ), cState ( cStateParam ) {}


        virtual ~ QTaskDialogButton() {}
    } ;

    typedef union
    {
        HICON hIcon;
        PCWSTR pszIcon;
    } QTaskDialogIcon;

    typedef QObArray<QTaskDialogButton> QTaskDialogButtonArray;
    typedef HRESULT ( WINAPI * QTaskDialogIndirectFunc )( const TASKDIALOGCONFIG * pTaskConfig, int * pnButton, int * pnRadioButton, BOOL * pfVerificationFlagChecked );

    TASKDIALOG_BUTTON * GetButtonData( const QTaskDialogButtonArray & oButtonArr ) const;
    INT_PTR GetButtonIndex( int nId,  const QTaskDialogButtonArray & oButtonArr ) const;

    void FillStruct( _Inout_ TASKDIALOGCONFIG & configTaskDialog );
    void FreeStruct( _Inout_ TASKDIALOGCONFIG & configTaskDialog );

    HWND m_hWnd;

    int m_nCommonButton;
    int m_nButtonDisabled;
    int m_nButtonElevation;

    int m_nFlags;
    int m_nWidth;
    int m_nDefaultCommandControl;
    int m_nDefaultRadioButton;

    QTaskDialogIcon m_mainIcon;
    QTaskDialogIcon m_footerIcon;

    QTaskDialogButtonArray m_aButtons;
    QTaskDialogButtonArray m_aRadioButtons;

    QString m_strContent;
    QString m_strMainInstruction;
    QString m_strTitle;
    QString m_strFooter;
    QString m_strVerification;
    QString m_strInformation;
    QString m_strExpand;
    QString m_strCollapse;

    int m_nProgressRangeMin;
    int m_nProgressRangeMax;
    int m_nProgressState; //ProgressBar or Marquee state
    int m_nProgressPos;  //ProgressBar pos or Marquee speed

    BOOL m_bVerified;
    int m_nRadioId;
    int m_nButtonId;
};


}

}//micro